using System;
using System.Net;
using System.IO;

namespace c3o.Framework.Web
{
    public class WebHelper
    {
        public WebHelper()
        {            
        }

        /// <summary>
        /// Post data to a page
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="PostString"></param>
        public static void PostPage(string Url, string PostString, ref CookieContainer cookies)
        {           
            HttpWebRequest webRequest = WebRequest.Create(Url) as HttpWebRequest;

            // Read the response
            StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
            string responseData = responseReader.ReadToEnd();
            responseReader.Close();

            // extract the viewstate value and build out POST data            

            // have a cookie container ready to receive the forms auth cookie
            cookies = new CookieContainer();

            // Now post to the login form
            webRequest = WebRequest.Create(Url) as HttpWebRequest;
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.CookieContainer = cookies;

            // write the form values into the request message
            StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
            requestWriter.Write(PostString);
            requestWriter.Close();
                        
            // we don't need the contents of the response, just the cookie it issues
            webRequest.GetResponse().Close();            
        }



        /// <summary>
        /// Return results for page hit
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ReadPage(string url, ref CookieContainer cookies)
        {
            // Setup the request
            HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;
            wr.CookieContainer = cookies;

            StreamReader sr = new StreamReader(wr.GetResponse().GetResponseStream());

            // Read to the end
            string response = sr.ReadToEnd();
            sr.Close();

            return response;
        }



        /// <summary>
        /// Return binary array of file
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static byte[] ReadFile(string url, ref  CookieContainer cookies)
        {
            byte[] result;
            byte[] buffer = new byte[4096];

            HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;

            wr.CookieContainer = cookies;

            using (WebResponse response = wr.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        int count = 0;
                        do
                        {
                            count = responseStream.Read(buffer, 0, buffer.Length);
                            memoryStream.Write(buffer, 0, count);

                        } while (count != 0);

                        result = memoryStream.ToArray();

                    }
                }
            }

            return result;
        }


        /// <summary>
        /// Download and save file to disk
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filename"></param>
        public static void Download(string url, string filename, ref CookieContainer cookies)
        {
            byte[] FileData = ReadFile(url, ref cookies);

            // Create file
            FileStream fs = File.Create(filename);

            //Write File
            BinaryWriter bw = new BinaryWriter(fs);

            bw.Write(FileData);

            // Close up
            bw.Close();
            fs.Close();
        }
    }
}
